﻿@model DHQGHN.PM0.Models.Admin.UserInRoleModel
@{

    var lstUser = ViewBag.lstUser;
    var columnsMenu = 2;
    var rowsMenu = lstUser.Items.Count % 2 == 0 ? (lstUser.Items.Count / columnsMenu) : ((lstUser.Items.Count+1) / columnsMenu);

    //var rowsMenu = (int)Math.Round((double)lstUser.Items.Count / (double)columnsMenu + 0.5);
   // int index = 0;

}
@using (
        Html.BeginForm(
        actionName: "actionCreateOrUpdateUserInRole", 
        controllerName: "UserInRole", 
        routeValues: new { }, method: FormMethod.Post, 
        htmlAttributes: new { @id = "user-in-role-form", @class = "form-inline", @role = "form", @style = "width: 100%;" })
    )
{
    <div class="modal-body min-width">
        <div class="form-group">

            @Html.AntiForgeryToken()
            @Html.HiddenFor(m => m.UserInRoleID)
            <div >
                <label style = "width: 100px; font-size: 12px;" >Tên ứng dụng</label><span class="field-validation-error"> * </span>
                @Html.DropDownList("AppID", new SelectList(ViewBag.lstApp, "value", "text", 0), new { @id = "input-code-app", @name = "code", @class = "SelectBoxContent", @disabled = "disabled", @style = "width: 260px !important;", @placeholder = "" })
            </div>
            <div>
                @Html.LabelFor(m => m.RoleID, new { @class = "labels", style = "width:100px;" })<span class="field-validation-error"> * </span>
                @Html.DropDownListFor(m => m.RoleID, new SelectList(ViewBag.lstRole, "value", "text", 0), new { @class = "SelectBoxContent", @placeholder = "", @style = "width: 260px !important;", @disabled = "disabled" })
            </div>
            <div>
                <label class="labels" style="width:100px">Người dùng</label>
                <div style="max-height: 200px; overflow: auto; resize: none;">
                    @if (!(lstUser.Items.Count == 0 || lstUser == null))
                    {
                        <table class="tbl-list" style="margin-left: 115px; width:80%">
                            @for (int i = 0; i < rowsMenu; i++)
                            {
                                <tr>
                                    @for (int j = 0; j < columnsMenu; j++)
                                    {
                                        int index = columnsMenu * i + j;
                                        if (index >= lstUser.Items.Count) { break; }
                                        <td style="width:30%" class="td-onclick">
                                            <div id="checkboxUserChecked">
                                                <input type="checkbox" name="@lstUser.Items[index].Value" />&nbsp;&nbsp;&nbsp;@lstUser.Items[index].Text
                                            </div>
                                        </td>

                                    }
                                </tr>
                            }
                        </table>
                    }
                    else
                    {
                        <span class="field-validation-error" style="margin-left:120px"> Vai trò đã thêm hết người dùng </span>
                    }
                </div>
            </div>
            <div>
                <br />
                @Html.LabelFor(m => m.Descriptions, new { @class = "labels", @for = "descriptions", style = "width:100px;" }) <span> &nbsp; </span>
                @Html.TextAreaFor(m => m.Descriptions, new { @class = "TextBoxContent", style = "width: 80%; height:150px; vertical-align:top; resize:none", @placeholder = "", @cols = 80, @rows = 10 })
                <br />
                @Html.ValidationMessageFor(m => m.Descriptions, null, new { @class = "Validation_Error", @style = "margin-left:120px" })
            </div>
        </div>
    </div>
}


<div id="overlay" class="web_dialog_overlay"></div>
@Scripts.Render("~/bundles/jqueryval")
<script>
        @functions{
            public string GetAntiForgeryToken()
            {
                string cookieToken, formToken;
                AntiForgery.GetTokens(null, out cookieToken, out formToken);
                return cookieToken + "," + formToken;
            }
        }
</script>
<script>
    $('#input-code-app').val(@ViewBag.AppID);
    var $userInRoleForm = $("#user-in-role-form");
    function save() {
        var result = false;
        $("#RoleID").removeAttr("disabled");
        var listID = getListUserInRoleID();
        
        if ($userInRoleForm.valid() && $('#RoleID').val().trim() > 0) {
            $.ajax({
                contentType: "application/json",
                type: "POST",
                url: $userInRoleForm.attr('action'),
                data: JSON.stringify({
                    RoleID: $('#RoleID').val().trim(),
                    listID: listID,
                    Descriptions: $('#Descriptions').val()
                }),
                dataType: 'json',//chú ý là form đặc biệt thì phải điền trường này
                async: false,
                headers: {
                    'VerificationToken': '@GetAntiForgeryToken()'
                },
                success: function (response) {
                    if (response != null && response.status == 200) {
                        window.notice(response.result, window.notice_success);
                        OnCriteria(crrPageIndex);
                        result = true;
                    } else {
                        window.notice(response.result, window.notice_error);
                        //$("#RoleID").attr("disabled", "disabled");
                    }
                }
            });
        }
        return result;
    }

    $('.td-onclick').click(function () {
        $(this).find(':checkbox').click();
    });

    $(":checkbox").click(function (e) {
        e.stopPropagation();
    });

    function getListUserInRoleID() {
        var values = [];
        $('#checkboxUserChecked input:checked').each(function () {
            values.push(this.name);
        });
        return values;
    }
</script>
